# -*- coding: utf-8 -*-
#accuracyclass.py

import os
import csv


#CSV_FILENAME = os.path.join(os.path.dirname(os.path.abspath(dimcalc.__file__)),'data/accuracyclass.csv')
CSV_FILENAME = 'accuracyclass.csv'

class AccuracyClass():
    def __init__(self, args):
        '''
        name, min_dim, max_dim, up_tolerance, un_tolerance, hole, shaft, recomended
        '''
        self.name = args[0]
        self.min_dim = float(args[1])
        self.max_dim = float(args[2])
        self.up_tolerance = float(args[3])
        self.un_tolerance = float(args[4])
        #self.hole = args[5]
        #self.shaft = args[6]
        #self.recomended = args[7]
    def __str__(self):
        return self.name+','+str(self.min_dim)+','+str(self.max_dim)+','+str(self.up_tolerance)+','+str(self.un_tolerance)

accuracyclass_list = []
accuracyclasses = []

reader = csv.reader(open(CSV_FILENAME, "r"))

for row in reader:
    if not (row[0] in accuracyclasses):
        accuracyclasses.append(row[0])

    accuracyclass_list.append(AccuracyClass(row))

def get_tolerance(name, value):
    for accuracyclass in accuracyclass_list:
        if accuracyclass.name == name:
            if (value > accuracyclass.min_dim) & (value <= accuracyclass.max_dim):
                return (accuracyclass.up_tolerance, accuracyclass.un_tolerance)
    return (0, 0)

accuracyclasses = sorted(accuracyclasses)
